package com.epro.dx.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.OrderModel.Liness;

/**
 * 订单详情商品列表适配器
 * 
 * @author qsc255
 * @version [版本号, 2015-2-3]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class OrderProductAdapter extends BaseAdapter {
	/**
	 * 上下文环境
	 */
	private Context mContext;

	private List<Liness> list;

	private String CurrencyCode;
	
	private DisplayImageOptions options;

	public OrderProductAdapter(Context context, List<Liness> list,
			String CurrencyCode) {
		super();
		this.mContext = context;
		this.list = list;
		this.CurrencyCode = CurrencyCode;
		options=ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {

		return null == list ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_orderproductlist, null);
			viewHolder = new ViewHolder();

			viewHolder.productIcon = (ImageView) convertView
					.findViewById(R.id.iv_img_product);
			viewHolder.product_sku = (TextView) convertView
					.findViewById(R.id.sku_number);
			viewHolder.product_name = (TextView) convertView
					.findViewById(R.id.tv_productname);
			viewHolder.product_price = (TextView) convertView
					.findViewById(R.id.tv_prodcut_price);
			viewHolder.quantity_count = (TextView) convertView
					.findViewById(R.id.tv_shipping);
			viewHolder.freeIcon = (ImageView) convertView
					.findViewById(R.id.product_free_mark);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Liness liness = list.get(position);
		if (null != liness) {

			String url = liness.getSmileImageUrl();
			if (URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) {
				viewHolder.productIcon.setTag(url);
				ImageLoader.getInstance().displayImage(url, viewHolder.productIcon, options);
			} else {
				viewHolder.productIcon.setImageBitmap(ActivityUtil
						.getDefault488(mContext.getApplicationContext()));
			}

			// 订单详情商品SKU
			viewHolder.product_sku.setText(String.valueOf(liness
					.getSku()));
			// 商品name
			viewHolder.product_name.setText(liness.getName());
			// 商品价格
			viewHolder.product_price.setText(CurrencyCode
					+ String.format("%.2f", liness.getUnitPrice()));
			if(liness.isGift()){
				viewHolder.quantity_count.setText(String.valueOf(liness
						.getGiftQty()));
				viewHolder.freeIcon.setVisibility(View.VISIBLE);
			}else{
				// 商品数量
				viewHolder.quantity_count.setText(String.valueOf(liness
						.getOrderedQty()));
				viewHolder.freeIcon.setVisibility(View.GONE);
			}
			
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView productIcon;

		ImageView freeIcon;

		TextView product_sku;

		TextView product_name;

		TextView product_price;

		TextView quantity_count;
	}
}
